package com.tanhua.dubbo.server.service;


import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;

import com.alibaba.dubbo.config.annotation.Reference;

import com.tanhua.common.vo.ErrorResult;



import com.tanhua.dubbo.server.api.UserLogApi;

import com.tanhua.dubbo.server.pojo.UserLog;
import com.tanhua.dubbo.server.vo.ManageLogVo;

import com.tanhua.dubbo.server.vo.PageResult;


import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ManagerLogService {
    //注入dubbo接口
    @Reference
    private UserLogApi userLogApi;

    //查询全部日志方法
    public PageResult findAllLogs(Integer page, Integer pagesize, Long uid, String sortProp, String sortOrder) {
        //设置分页
        PageResult pageResult = new PageResult();
        pageResult.setPage(page);
        pageResult.setPagesize(pagesize);
        //查询
        List<UserLog> logList = userLogApi.queryLogs(page, pagesize, uid, sortProp, sortOrder);

        if (ObjectUtil.isEmpty(logList)) {
            ErrorResult.findLogsError();
        }
        List<ManageLogVo> manageLogVoList = new ArrayList<>();

        for (UserLog log : logList) {
            System.out.println(log);
            ManageLogVo manageLogVo = new ManageLogVo();//创建ManageLogVo对象
            manageLogVo.setId(Convert.toInt(log.getLid()));//日志编号
            manageLogVo.setEquipment("iphone18");//设备
            manageLogVo.setLogTime(log.getCreated());//登录时间
            manageLogVo.setPlace(log.getAddress());//地址
            pageResult.setCounts(logList.size());//日志数量
            manageLogVoList.add(manageLogVo);
        }
        pageResult.setItems(manageLogVoList);
        return pageResult;
    }


}
